//
//  WKPopView.m
//  Walk
//
//  Created by cesh on 16/9/21.
//  Copyright © 2016年 cesh. All rights reserved.
//

#import "WKPopView.h"
#import "WKChatViewController.h"
#import <Masonry.h>

@interface WKPopView()

@property (strong, nonatomic) UIToolbar *glassBG;

@end

@implementation WKPopView

+ (instancetype)showWithType:(PopViewType)popViewType {
    WKPopView *popV = [[WKPopView alloc]init];
    popV.popViewType = popViewType;
    
    [[UIApplication sharedApplication].keyWindow addSubview:popV];
    return popV;
}

+ (void)dismiss {
    NSMutableArray <WKPopView *> *popVArray = [NSMutableArray array];
    WKPopView *popV = [[WKPopView alloc]init];
    for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([view isKindOfClass:[WKPopView class]]) {
            popV = (WKPopView *)view;
            [popVArray addObject:popV];
        }
    }
    for (WKPopView *popV in popVArray) {
        [popV removeFromSuperview];
    }
}

- (instancetype)init {
    if (self = [super init]) {
        self.frame = [UIScreen mainScreen].bounds;
        [self setupBlur];
        [self addFrogView];
    }
    return self;
}

#pragma mark - 添加毛玻璃
- (void)setupBlur {
    
    // 1.创建toolBar
    UIToolbar *toolBar = [[UIToolbar alloc] init];
    toolBar.alpha = 0.95;
    [self addSubview:toolBar];
    
    // 1.1改变样式
    toolBar.barStyle = UIBarStyleBlack;
    toolBar.translucent = YES;
    
    // 2.添加约束
    [toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    
    [UIView animateWithDuration:0.5 animations:^{
        toolBar.alpha = 1.0;
    }];
    self.glassBG = toolBar;
}

- (void)addFrogView {
    UIImageView *centerView = [[UIImageView alloc]init];
    centerView.image = [UIImage imageNamed:@"popOverImg"];
    centerView.size = CGSizeMake(200, 236);
    centerView.center = CGPointMake(screenW * 0.5, screenH * 0.5);
    centerView.userInteractionEnabled = YES;
    self.centerView = centerView;
    [self addSubview:centerView];
}

- (void)setPopViewType:(PopViewType)popViewType {
    _popViewType = popViewType;
    switch (popViewType) {
        case PopViewTypeLoading:
        {
            //气泡界面提醒有活动正在进行
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"正在拼命加载中...";
            label1.font = [UIFont systemFontOfSize:16.0];
            label1.textColor = [UIColor colorWithRed:0x4a /255.0 green:0x4a /255.0 blue:0x4a /255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.5);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"请稍等片刻哦~";
            label2.font = [UIFont systemFontOfSize:16.0];
            label2.textColor = [UIColor colorWithRed:0x4a /255.0 green:0x4a /255.0 blue:0x4a /255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 15 + label2.height);
            
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
        }
        break;
        case PopViewTypeGoChat:
        {
            //气泡界面提醒有活动正在进行
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"你还有正在进行的活动";
            label1.font = [UIFont systemFontOfSize:14.0];
            label1.textColor = [UIColor colorWithRed:0x4a /255.0 green:0x4a /255.0 blue:0x4a /255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.5);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"点击下方按钮恢复哦~";
            label2.font = [UIFont systemFontOfSize:14.0];
            label2.textColor = [UIColor colorWithRed:0x4a /255.0 green:0x4a /255.0 blue:0x4a /255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 8 + label2.height);
            
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
            
            UIButton *goChat = [[UIButton alloc]init];
            [goChat setBackgroundImage:[UIImage imageNamed:@"popOverBtn_black"] forState:UIControlStateNormal];
            [goChat setTitle:@"立即恢复" forState:UIControlStateNormal];
            [goChat setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [goChat.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
            goChat.size = CGSizeMake(160, 34);
            goChat.center = CGPointMake(self.centerView.width * 0.5, label2.bottom + goChat.height + 17);
            
            [goChat addTarget:self action:@selector(goChat) forControlEvents:UIControlEventTouchUpInside];
            [self.centerView addSubview:goChat];
        }
            break;
        case PopViewTypeMatchSuccess:
        {
            UIImageView *star = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"star"]];
            [star sizeToFit];
            star.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.4);
            
            
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"恭喜你组队成功";
            label1.font = [UIFont systemFontOfSize:16.0];
            label1.textColor = [UIColor colorWithRed:0x4a /255.0 green:0x4a /255.0 blue:0x4a /255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.6);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"即将进入聊天";
            label2.font = [UIFont systemFontOfSize:16.0];
            label2.textColor = [UIColor colorWithRed:0x4a /255.0 green:0x4a /255.0 blue:0x4a /255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 8 + label2.height);
            
            [self.centerView addSubview:star];
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
        }
            break;
        case PopViewTypeQuitChat:
        {
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"温馨提示";
            label1.font = [UIFont systemFontOfSize:16.0];
            label1.textColor = [UIColor colorWithRed:0x27 / 255.0 green:0x88 / 255.0 blue:0xff / 255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.3);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"退出聊天即表示不与之组局噢~";
            label2.font = [UIFont systemFontOfSize:12.0];
            label2.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 10 + label2.height * 0.5);
            
            UILabel *label3 = [[UILabel alloc]init];
            label3.text = @"退出后不可恢复！";
            label3.font = [UIFont systemFontOfSize:12.0];
            label3.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label3 sizeToFit];
            label3.center = CGPointMake(self.centerView.width * 0.5, label2.bottom + 6 + label3.height * 0.5);
            
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
            [self.centerView addSubview:label3];
            
            UIButton *confirm = [[UIButton alloc]init];
            [confirm setBackgroundImage:[UIImage imageNamed:@"popOverBtn_black"] forState:UIControlStateNormal];
            [confirm setTitle:@"确认退出" forState:UIControlStateNormal];
            [confirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [confirm.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
            [confirm sizeToFit];
            [confirm addTarget:self action:@selector(confirmQuitChat) forControlEvents:UIControlEventTouchUpInside];
            confirm.center = CGPointMake(self.centerView.width * 0.5, label3.bottom + 16 + confirm.height * 0.5);
            
            UIButton *cancel = [[UIButton alloc]init];
            [cancel setBackgroundImage:[UIImage imageNamed:@"popOverBtn_grey"] forState:UIControlStateNormal];
            [cancel setTitle:@"我再想想" forState:UIControlStateNormal];
            [cancel setTitleColor:[UIColor colorWithRed:0x9b / 255.0 green:0x9b / 255.0 blue:0x9b / 255.0 alpha:0x9b / 255.0] forState:UIControlStateNormal];
            [cancel.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
            [cancel sizeToFit];
            cancel.center = CGPointMake(self.centerView.width * 0.5, confirm.bottom + 10 + confirm.height * 0.5);
            [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
            [self.centerView addSubview:confirm];
            [self.centerView addSubview:cancel];
        }
            break;
        case PopViewTypeQuitRecruit:
        {
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"温馨提示";
            label1.font = [UIFont systemFontOfSize:18.0];
            label1.textColor = [UIColor colorWithRed:0x27 / 255.0 green:0x88 / 255.0 blue:0xff / 255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.5);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"已成功停止招募";
            label2.font = [UIFont systemFontOfSize:14.0];
            label2.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 15 + label2.height * 0.5);
            
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
        }
            break;
        case PopViewTypeNotMatchYet:
        {
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"温馨提示";
            label1.font = [UIFont systemFontOfSize:18.0];
            label1.textColor = [UIColor colorWithRed:0x27 / 255.0 green:0x88 / 255.0 blue:0xff / 255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.5);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"要先匹配成功";
            label2.font = [UIFont systemFontOfSize:14.0];
            label2.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 15 + label2.height * 0.5);
            
            UILabel *label3 = [[UILabel alloc]init];
            label3.text = @"才能进入聊天室哦";
            label3.font = [UIFont systemFontOfSize:14.0];
            label3.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label3 sizeToFit];
            label3.center = CGPointMake(self.centerView.width * 0.5, label2.bottom + 15 + label3.height * 0.5);
            
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
            [self.centerView addSubview:label3];
        }
            break;
        case PopViewTypeFeedback:
        {

        }
            break;
        case PopViewTypeFeedbackSuccess:
        {
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"感谢您的反馈";
            label1.font = [UIFont systemFontOfSize:18.0];
            label1.textColor = [UIColor colorWithRed:0x27 / 255.0 green:0x88 / 255.0 blue:0xff / 255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.5);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"阿里嘎多";
            label2.font = [UIFont systemFontOfSize:14.0];
            label2.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 15 + label2.height * 0.5);
            
            UILabel *label3 = [[UILabel alloc]init];
            label3.text = @"过砸一玛斯！";
            label3.font = [UIFont systemFontOfSize:14.0];
            label3.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label3 sizeToFit];
            label3.center = CGPointMake(self.centerView.width * 0.5, label2.bottom + 15 + label3.height * 0.5);
            
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
            [self.centerView addSubview:label3];
        }
            break;
        case PopViewTypeTimeOut:
        {
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"哎呀！";
            label1.font = [UIFont systemFontOfSize:18.0];
            label1.textColor = [UIColor colorWithRed:0x27 / 255.0 green:0x88 / 255.0 blue:0xff / 255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.5);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"数据请求";
            label2.font = [UIFont systemFontOfSize:14.0];
            label2.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 15 + label2.height * 0.5);
            
            UILabel *label3 = [[UILabel alloc]init];
            label3.text = @"貌似超时了呢~!";
            label3.font = [UIFont systemFontOfSize:14.0];
            label3.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label3 sizeToFit];
            label3.center = CGPointMake(self.centerView.width * 0.5, label2.bottom + 15 + label3.height * 0.5);
            
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
            [self.centerView addSubview:label3];
        }
            break;
        case PopViewTypeBindSuccess: {
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"恭喜您！";
            label1.font = [UIFont systemFontOfSize:18.0];
            label1.textColor = [UIColor colorWithRed:0x27 / 255.0 green:0x88 / 255.0 blue:0xff / 255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.5);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"手机号码绑定成功";
            label2.font = [UIFont systemFontOfSize:14.0];
            label2.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 15 + label2.height * 0.5);
            
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
        }
            break;
        case PopViewTypeJustBlur: {
            self.centerView.hidden = YES;
        }
            break;
        case PopViewTypeCanNotRecruitMore:{
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"不好意思";
            label1.font = [UIFont systemFontOfSize:18.0];
            label1.textColor = [UIColor colorWithRed:0x27 / 255.0 green:0x88 / 255.0 blue:0xff / 255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.5);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"当前您似乎";
            label2.font = [UIFont systemFontOfSize:14.0];
            label2.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 15 + label2.height * 0.5);
            
            UILabel *label3 = [[UILabel alloc]init];
            label3.text = @"没有招募更多成员的权限";
            label3.font = [UIFont systemFontOfSize:14.0];
            label3.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label3 sizeToFit];
            label3.center = CGPointMake(self.centerView.width * 0.5, label2.bottom + 15 + label3.height * 0.5);
            
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
            [self.centerView addSubview:label3];
        }
            break;
        case PopViewTypeLeftScroll: {
            
            
            
            
            
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"点' x '或者左滑";
            label1.font = [UIFont systemFontOfSize:18.0];
            label1.textColor = [UIColor colorWithRed:0x27 / 255.0 green:0x88 / 255.0 blue:0xff / 255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.4);
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"即表示暂不约他／她哟";
            label2.font = [UIFont systemFontOfSize:14.0];
            label2.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 15 + label2.height * 0.5);
            
            UIButton *goChat = [[UIButton alloc]init];
            [goChat setBackgroundImage:[UIImage imageNamed:@"popOverBtn_black"] forState:UIControlStateNormal];
            [goChat setTitle:@"我知道了" forState:UIControlStateNormal];
            [goChat setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [goChat.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
            goChat.size = CGSizeMake(160, 34);
            goChat.center = CGPointMake(self.centerView.width * 0.5, label2.bottom + goChat.height + 17);
            [goChat addTarget:self action:@selector(dismissPopOverView) forControlEvents:UIControlEventTouchUpInside];
            
            [self.centerView addSubview:goChat];
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
        }
            break;
        case PopViewTypeRightScrool: {
            UILabel *label1 = [[UILabel alloc]init];
            label1.text = @"点'    '或者右滑";
            label1.font = [UIFont systemFontOfSize:18.0];
            label1.textColor = [UIColor colorWithRed:0x27 / 255.0 green:0x88 / 255.0 blue:0xff / 255.0 alpha:1.0];
            [label1 sizeToFit];
            label1.center = CGPointMake(self.centerView.width * 0.5, self.centerView.height * 0.4);
            
            UIImageView *gougou = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gougou"]];
            gougou.center = CGPointMake(self.centerView.width * 0.5 - 28, self.centerView.height * 0.4);
            
            
            UILabel *label2 = [[UILabel alloc]init];
            label2.text = @"即表示应邀等待对方的回应";
            label2.font = [UIFont systemFontOfSize:14.0];
            label2.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
            [label2 sizeToFit];
            label2.center = CGPointMake(self.centerView.width * 0.5, label1.bottom + 15 + label2.height * 0.5);
            UIButton *goChat = [[UIButton alloc]init];
            [goChat setBackgroundImage:[UIImage imageNamed:@"popOverBtn_black"] forState:UIControlStateNormal];
            [goChat setTitle:@"我知道了" forState:UIControlStateNormal];
            [goChat setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [goChat.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
            goChat.size = CGSizeMake(160, 34);
            goChat.center = CGPointMake(self.centerView.width * 0.5, label2.bottom + goChat.height + 17);
            [goChat addTarget:self action:@selector(dismissPopOverView) forControlEvents:UIControlEventTouchUpInside];
            
            [self.centerView addSubview:gougou];
            [self.centerView addSubview:goChat];
            [self.centerView addSubview:label1];
            [self.centerView addSubview:label2];
        }
            break;
        default:
            break;
    }
}

- (void)dismissPopOverView {
    [WKPopView dismiss];
}
#pragma mark - 代理方法
- (void)goChat {
    //进入聊天界面
    if ([self.delegate respondsToSelector:@selector(goChatVC)]) {
        [self.delegate goChatVC];
    }
}

- (void)confirmQuitChat {
    //退出组局
    if ([self.delegate respondsToSelector:@selector(quitChat)]) {
        [self.delegate quitChat];
    }
}

- (void)cancel {
    //取消退出组局
    if ([self.delegate respondsToSelector:@selector(cancelQuitChat)]) {
        [self.delegate cancelQuitChat];
    }
}



@end
